package com.yanqu.road.dataHelper.action;

import com.yanqu.road.dataHelper.action.excel.Rebate;
import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.manager.ServerMgr;
import com.yanqu.road.entity.servercenter.ServerInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

public class RebateExcel extends actionBase{

    public void go(List<Long> serverList){
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        List<Future> futureList = new ArrayList<>();
        for(Long serverId : serverList){
            if(ServerMgr.getServerInfoList().containsKey(serverId)){
                ServerInfo serverInfo = ServerMgr.getServerInfoList().get(serverId);
                Future future = executorService.submit(new Callable<Boolean>() {
                    @Override
                    public Boolean call() throws Exception {
                        GameServer testGameServer = new GameServer(serverInfo);
                        Rebate rebate = new Rebate(testGameServer);
                        rebate.go1();
                        return true;
                    }
                });
                futureList.add(future);
            }
        }
        isResult(futureList);
        Map<String,Map<String,String>> openIdMap = new ConcurrentHashMap<>();
        List<Map<String,String>> outData = DataMgr.getData();
        //所有区服合并成一个
        //platformUserId    openId    vipExp    buySecret    userId    buyMonth    buyFund    buyYear
        for(Map<String,String> one : outData){
            if(!openIdMap.containsKey(one.get("openId"))){
                one.put("userCount","1");
                openIdMap.put(one.get("openId"),one);
            }else{
                Map<String,String> oldOne = openIdMap.get(one.get("openId"));
                long oldVipExp = Long.valueOf(oldOne.get("vipExp"));
                long newVipExp = Long.valueOf(one.get("vipExp"));
                openIdMap.get(one.get("openId")).put("vipExp",String.valueOf(oldVipExp + newVipExp));
                openIdMap.get(one.get("openId")).put("userId",oldOne.get("userId") + "," + one.get("userId"));
                openIdMap.get(one.get("openId")).put("userCount",String.valueOf(Integer.valueOf(oldOne.get("userCount")) + 1));

                if(Long.valueOf(one.get("buySecret")) == 1L){
                    openIdMap.get(one.get("openId")).put("buySecret","1");
                }
                if(Long.valueOf(one.get("buyFund")) == 1L){
                    openIdMap.get(one.get("openId")).put("buyFund","1");
                }
                if(Long.valueOf(one.get("buyYear")) == 1L){
                    openIdMap.get(one.get("openId")).put("buyYear","1");
                }
                if(Long.valueOf(one.get("buyMonth")) == 1L){
                    openIdMap.get(one.get("openId")).put("buyMonth","1");
                }

            }
        }
        DataMgr.clearExcelData();
        DataMgr.addData(new ArrayList<>(openIdMap.values()));
        DataMgr.outText("国服返利");
    }
}
